package com.atzxh.guigu.juc.auxiliary;

import java.util.concurrent.CyclicBarrier;

/**
 * @author 小红同学
 * @create 2023-04-27-22:06
 * @description     CyclicBarrier 循环栅栏演示
 */
public class CyclicBarrierDemo {
    // 设置一个固定值
    public static final int NUM = 7;

    public static void main(String[] args) {

        // 创建 CyclicBarrier 对象
        CyclicBarrier cyclicBarrier = new CyclicBarrier(NUM,()->{
            System.out.println("******七龙珠收集完毕");
        });

        // 循环创建线程
        for (int i = 1; i <= 7 ; i++) {
            new Thread(()->{
                try {
                    System.out.println(Thread.currentThread().getName() + "  号龙珠已收集");
                    cyclicBarrier.await();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            },String.valueOf(i)).start();
        }
    }
}
